//
//  ActivityDetailpageVC.m
//  ErWa
//
//  Created by 赵颜彬 on 16/3/10.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ActivityDetailpageVC.h"
#import <UIImageView+WebCache.h>
@interface ActivityDetailpageVC ()<UIWebViewDelegate>
@property (nonatomic, strong) UIImageView *backImg;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *contentImg;/**< 放内容的视图 */
@property (nonatomic, strong) UIWebView *contentView;/**< 内容的webview */
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic ,strong) UILabel *addressLabel;
@property (nonatomic, strong) UIScrollView *scrollview;/**< 滚动视图 */

@end

@implementation ActivityDetailpageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // navigation设置
    self.navigationItem.title = @"活动详情";
    
    [self createSubviews];

}

/// 视图创建
- (void)createSubviews
{
    // 内容的webview
    self.contentView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [_contentView loadHTMLString:_modal.content baseURL:nil];
    
    // 设置代理人
    _contentView.delegate = self;
    _contentView.scrollView.contentInset = UIEdgeInsetsMake(self.view.frame.size.height / 2, 0, 0, 0);
    [self.view addSubview:_contentView];
    
    // 用于放置webview上方所有视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, - self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2)];
    [self.contentView.scrollView addSubview:view];
    view.backgroundColor = [UIColor whiteColor];
    
    // 上方背景图
    self.backImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, view.frame.size.width - 20, view.frame.size.height / 2)];
    _backImg.layer.cornerRadius = 5;
    _backImg.clipsToBounds = YES;
    [view addSubview:_backImg];
    // 标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,_backImg.frame.origin.y + _backImg.frame.size.height + 10, view.frame.size.width - 20, _backImg.frame.size.height / 2)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:19];
    [view addSubview:_titleLabel];
    
    // 日期
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, self.view.frame.size.width - 20, (view.frame.size.height / 2 - _titleLabel.frame.size.height - 25) / 2)];
    _dateLabel.font = [UIFont systemFontOfSize:15];
    _dateLabel.textColor = [UIColor grayColor];
    [view addSubview:_dateLabel];
    //地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _dateLabel.frame.origin.y + _dateLabel.frame.size.height + 5 , self.view.frame.size.width - 20, _dateLabel.frame.size.height)];
    _addressLabel.font = [UIFont systemFontOfSize:15];
    _addressLabel.textColor = [UIColor grayColor];
    [view addSubview:_addressLabel];
    
    //给各个label赋值
    [_backImg sd_setImageWithURL:[NSURL URLWithString:_modal.background]];
    _titleLabel.text = _modal.name;
    _dateLabel.text = _modal.time;
    _addressLabel.text = _modal.areaDetail;

}
//要获取webView内容页的总高度，而因为可能网速,图片这些原因，他每次的contentsize是实时变化的，所以我们要监听webView的scrollView的contentSize的变化
- (void)addObserverForWebViewContentSize{
    
    [self.contentView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:nil];
    
}

- (void)removeObserverForWebViewContentSize{
    
    [self.contentView.scrollView removeObserver:self forKeyPath:@"contentSize"];
    
}

// 监听结果回调事件
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{
    
    //在这里边添加你的代码
    
    
    [self layoutCell];
    
}

//设置footerView的合理位置

- (void)layoutCell{
    
    //取消监听，因为这里会调整contentSize，避免无限递归
    
    [self removeObserverForWebViewContentSize];
    
    CGSize contentSize =self.contentView.scrollView.contentSize;
    
    
    
    // 日期
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _contentView.frame.size.height, self.view.frame.size.width - 20, self.view.frame.size.height / 20)];
    _dateLabel.font = [UIFont systemFontOfSize:15];
    _dateLabel.backgroundColor = [UIColor greenColor];
    _dateLabel.textColor = [UIColor grayColor];
    [_contentView.scrollView addSubview:_dateLabel];
    //地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _dateLabel.frame.origin.y + _dateLabel.frame.size.height + 5 , self.view.frame.size.width - 20, _dateLabel.frame.size.height)];
    _addressLabel.font = [UIFont systemFontOfSize:15];
    _addressLabel.backgroundColor = [UIColor orangeColor];
    _addressLabel.textColor = [UIColor grayColor];
    [_contentView.scrollView addSubview:_addressLabel];
    
    
    self.contentView.scrollView.contentSize = CGSizeMake(contentSize.width, contentSize.height + 150);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
